<?php
/*
need FLV programming?
FLV Hosting 
*/

	require '../config.php' ;
	//if(!isset($_SERVER['PHP_AUTH_USER']) || ($_SERVER['PHP_AUTH_USER']!=_ADMIN_LOGIN || $_SERVER['PHP_AUTH_PW'] != _ADMIN_PASSWORD)) {
	   //header('WWW-Authenticate: Basic realm="Administration"');
	   //header('HTTP/1.0 401 Unauthorized');
	   //echo '<p><center><b>This site requires a valid login and password.</b></center>';
	   //exit;
	 //}

	$fld = !$_REQUEST[fld] ? 'images' : 'images/' . escapeshellcmd($_REQUEST[fld]); 

	function remove_file($fl) {
		$fl = escapeshellcmd($fl);
		if(file_exists(_APP . '/' . $fl )) {
			unlink(_APP . '/' . $fl);
			return 'File deleted' ;
		}
		return 'File does not exist' ;
	}

	function get_folders($selected='') {
		$ret = array(); $str = '';
		if($dirstream = @opendir(_APP.'/images')){
			clearstatcache() ;
			while (false !== ($filename = readdir($dirstream))) {
			   if($filename!="." && $filename!="..") {
				   if (is_dir(_APP.'/images/'.$filename))
					   $ret[] = $filename ;
			   }
		   }
		}
		@closedir($dirstream);
		sort($ret, SORT_STRING);
		reset($ret);
		for($i=0; $i<count($ret); $i++) {
			$sel = $selected == 'images/'.$ret[$i] ? ' selected' : '';
			$str .= '<option value="'.$ret[$i].'"'. $sel . '>images/'.$ret[$i].'</option>'._E;
		}
		return $str;
	}

	function create_folder($name, &$fld) {
		$name = escapeshellcmd($name);
		clearstatcache();
		$newfldr = _APP . '/images/' . $name ;
		if(!trim($name)||eregi('[^[:alnum:]_]',$name)) {
			return 'Bad folder name' ;
		}
		elseif(file_exists($newfldr)) {
			return sprintf('Folder "%s" exists',$name) ;
		}
		else {
			mkdir($newfldr,0777);
			$fld = 'images/' . $name ;
		}
		return sprintf('Folder "%s" created', $name) ;
	}

	function get_folder_content($fld) {
		$ret = array(); $str = '';
		if($dirstream = opendir(_APP.'/'. $fld)){
			clearstatcache() ;
			while (false !== ($filename = readdir($dirstream))) {
			   if(is_file(_APP.'/'.$fld.'/'.$filename) && $filename != "." && $filename != ".." && eregi('(\.png|\.jpg|\.gif)$',$filename)) {
					 $ret[] = $filename ;
			   }
		   }
		}
		@closedir($dirstream);
		if(!empty($ret)) {
			sort($ret,SORT_STRING);
		}
		for($i=0; $i<count($ret); $i++) {
			$str .= '<tr class="tbrow"><td><a href="javascript: void(0);" class="lnk" onclick="parent.document.all.imgshow.src=\''.('../'.$fld.'/'.$ret[$i]).'\';" title="Click to preview">'. $ret[$i] . '</a></td><td align="center">[<a href="javascript: void(0);" onclick="ret_val(\''.$fld.'/'.$ret[$i].'\')" title="Insert into html" class="lnk">Ins</a>] [<a href="'._ME.'?del='.$fld.'/'.$ret[$i].'&fld='.($fld=='images'?'':$fld).'" onclick="return confirm(\'Are you sure?\')" title="Delete from server" class="lnk">Del</a>]</td></tr>'._E;
		}
		return $str;
	}
	
	function save_file($fl, $fld) {
		
		$fld = escapeshellcmd($fld);
//		if(!$fld) $fld = 'images';
//		else $fld = 'images/' . $fld;
		
		if(file_exists(_APP . '/'. $fld . '/'. $fl['u_file']['name'])) {
			@unlink($fl['u_file']['tmp_name']);
			return sprintf('File "%s" already exists',$fl['u_file']['name']) ;
		}
		if($fl['u_file']['error']) { 
				@unlink($fl['u_file']['tmp_name']);
				return sprintf('Upload error: %s', $fl['u_file']['error']) ; 
		}
		if(!ereg('\.(jpeg|jpg|gif|png|bmp)$',$fl['u_file']['name'])) {
			@unlink($fl['u_file']['tmp_name']);
				return sprintf('Upload error: the file you are trying to upload must have a jpeg, jpg, gif, png or bmp extension!') ; 
		} // i f
		if (is_uploaded_file($fl['u_file']['tmp_name'])){
			$wrn = @move_uploaded_file($fl['u_file']['tmp_name'], _APP . '/'. $fld . '/'. $fl['u_file']['name']);
			if(!$wrn) {
				return 'File upload cannot be completed' ;
			}
			else {
				@chmod(_APP . '/'. $fld . '/'. $fl['u_file']['name'], 0777);
			} // e l s e
		}
		return 'File uploaded' ;
	}

	function clear_folder($fld) {
		
		$rem = escapeshellcmd($fld) ;
		$fld = 'images/'.$fld;
		$fld = _APP.'/'.escapeshellcmd($fld);
		if($dirstream = opendir($fld)){
			clearstatcache() ;
			while (false !== ($filename = readdir($dirstream))) {
			   if(is_file($fld.'/'.$filename) && $filename != "." && $filename != "..") {
					 unlink($fld.'/'.$filename) ;
			   }
		   }
		}
		@closedir($dirstream);
		if($rem != '') {
			rmdir($fld);
			return 'Folder deleted' ;
		}
		else {
			return 'Files cleared' ;
		}
		
	}

	# WORKAROUND
	if(isset($_POST[make_fld])) {
		$MSG = create_folder($_POST[make_fld], &$fld);	
	} // i f
	if(isset($_GET[del])) {
		$MSG = remove_file($_GET[del]);
	} // i f
	if(isset($_FILES[u_file])) {
		$MSG = save_file($_FILES, $fld);
	}
	if(isset($_REQUEST[delfld])) {
		//$MSG = $_REQUEST[delfld].'<br>' ;
		$MSG = clear_folder($_REQUEST[delfld]);
	}
	
	//$MSG = 'shit happens';
?>
<html>
<head>
<title>Images manager</title>
<style type="text/css" title="">
.usual {font-family: Arial; font-weight: bold; font-size: 8pt;}
.inv_form {padding: 0; margin: 0;}
.tbrow {font-family: Arial; font-size: 8pt;}
.lnk {font-family: Arial; font-size: 8pt; color: navy; text-decoration: none;}
</style>
</head>
<SCRIPT>
<!-- 
function ret_val(fl) {
	parent.returnValue=fl
	parent.close()
}
//-->
</SCRIPT>
<body>
<div name="total" id="total">
<div style="width: 100%; height: 10px; text-align: center; color: red; font-size: 8pt; font-family: Arial;">
<?php
	 if($MSG) {
	 	echo $MSG ;
	 } // i f
?>
</div>
<form method=post target="imgholder" action="img_library_left.php" class="inv_form" name="selform">
	<select name="fld" class="usual" style="width: 170px;" onchange="document.location.href = 'img_library_left.php?fld=' + this.form.fld.options[this.form.fld.selectedIndex].value;;">
		<option value="">images</option>
		<?=get_folders($fld)?>
	</select>
	<input type="button" value=" X " class="usual" onclick="if(confirm('This operation will erase the selected folder!')) {document.location.href = 'img_library_left.php?delfld=' + this.form.fld.options[this.form.fld.selectedIndex].value;}">
</form>
<br>
<table align="center" width=100% border=1 cellspacing=0 cellpadding=2 rules=both frame=void bordercolor=#FFFF99>
<?php
	 $ret = get_folder_content($fld);
	 echo $ret ? $ret : '<tr class="tbrow"><td align=center>nothing found</td></tr>' ;
?>	
</div>
</table>
</body>
</html>